########################################################################################
@model[][t]
########################################################################################

$account[^oSensus.data.dictionary[account]]
$issuer[^oSensus.data.dictionary[issuer]]

^if( def $form:account && def $form:issuer && $form:year && $form:quarter ){
	$activities[^table::sql{
		SELECT id, name
		FROM activities
		WHERE accountId = ^int:sql{ SELECT id FROM account WHERE name = '$form:account' }
		ORDER BY name
	}]
	$oSensus.view[issuerActivitiesEditor]
	$issuerActivities[^hash::sql{
		SELECT activitiesId, value
		FROM activitiesToIssuers ati, activities a
		WHERE issuerId = ^int:sql{ SELECT id FROM issuer WHERE name = '$form:issuer' }
		AND activitiesId = a.id
		AND a.accountId = ^int:sql{ SELECT id FROM account WHERE name = '$form:account' }
		AND year = '$form:year'
		AND quarter = '$form:quarter'
	}]
}

^if( def $form:saveActivities ){
	$t[^table::sql{ SELECT id FROM activities WHERE accountId = $form:accountId }]
	^t.menu{
		^void:sql{
			REPLACE INTO activitiesToIssuers
			SET
				issuerId = $form:issuerId,
				activitiesId = $t.id,
				year = '$form:year',
				quarter = '$form:quarter',
				value = '$form:[$t.id]'
		}
	}
	$isSaved(true)
}

########################################################################################



########################################################################################
@view[]
########################################################################################

<h1>Редактирование показателей эмитентов</h1>
<div class="wrapper">
	<table id="activitiesPicker">
		<tr>
			<td class="aRight">Отчет</td>
			<td id="account">^account.menu{ <span class="ajaxAction">$account.name</span> }[&nbsp]</td>		
		</tr>
		<tr>
			<td class="aRight">Эмитент</td>	
			<td id="issuer">
				<table>
					<tr valign="top">
						<td>
							^issuer.menu{
								<span class="ajaxAction">$issuer.name</span>
								^if( ^issuer.line[] % 8 == 0 ){ </td><td> }{ <br /> }
							}
						</td>
					</tr>
				</table></td>
		</tr>
		<tr>
			<td class="aRight">Год</td>
			<td id="year">^for[i](2000;2008){ <span class="ajaxAction">$i</span>&nbsp }</td>
		</tr>
		<tr>
			<td class="aRight">Квартал</td>
			<td id="quarter">^for[i](1;4){ <span class="ajaxAction">$i</span>&nbsp }</td>
		</tr>
	</table>
	<div id="issuerActivities" />
</div>

########################################################################################


########################################################################################
@issuerActivitiesEditor[]
########################################################################################

<table id="issuerActivitiesTable" width="50%">
	^activities.menu{
		<tr>
			<td class="aRight" width="60%">$activities.name</td>
			<td><input name="$activities.id" value="$issuerActivities.[$activities.id].value"/></td>
		</tr>
	}
	<tr>
		<td colspan="2" class="aRight">
			<input type="hidden" name="accountId" value="^int:sql{ SELECT id FROM account WHERE name = '$form:account' }" />
			<input type="hidden" name="issuerId" value="^int:sql{ SELECT id FROM issuer WHERE name = '$form:issuer' }" />
			<input type="hidden" name="year" value="$form:year" />
			<input type="hidden" name="quarter" value="$form:quarter" />
			<div class="green hidden">Показатели сохранены успешно</div>
			<input type="submit" onclick="saveActivities()" name="saveActivities" value="Сохранить" />
		</td>
	</tr>
</table>

########################################################################################